package com.example.accountprovider.dubbo;

import com.example.accountprovider.tcc.IAccountTccAction;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.example.api.AccountApiTcc;
import org.example.dto.AccountDto;
import org.example.response.R;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Kong
 * @date 2021-9-14 15:03
 */
@Slf4j
@DubboService
public class AccountDubboServiceTcc implements AccountApiTcc {
    @Autowired
    private IAccountTccAction accountTccAction;
    @Override
    public R decrease(AccountDto dto) {
        log.info("[TCC模式]全局事务XID:{}", RootContext.getXID());
        boolean result = accountTccAction.prepareDecreaseAccount(null,dto.getUserId(),dto.getBalance());
        if(result) {
            return R.Builder.getInstance().buildOk().build();
        }
        return R.Builder.getInstance().buildFailure(null).build();
    }
}
